package com.amos.teaching_aids.presenter;

import android.content.Context;
import android.widget.ListView;

import com.amos.teaching_aids.R;
import com.amos.teaching_aids.UserInfo;
import com.amos.teaching_aids.constant.URLConstant;
import com.amos.teaching_aids.model.IUserModel;
import com.amos.teaching_aids.model.UserModel;
import com.amos.teaching_aids.net.STNetTask;
import com.amos.teaching_aids.ui.Interface.IUserBetterView;
import com.amos.teaching_aids.ui.Interface.IUserLoginView;
import com.amos.teaching_aids.ui.Interface.IUserRegisterView;
import com.amos.teaching_aids.ui.adapter.AdapterBase;
import com.amos.teaching_aids.ui.adapter.AdapterUser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/10/8 0008.
 */

public class UserPresenter extends BasePresenter implements IUserPresenter, UserModel.onLoginListener,
UserModel.onRegListener, UserModel.onLogoutListener{
    private IUserBetterView mUserBetterView;
    private IUserLoginView mUserLoginView;
    private IUserRegisterView mUserRegView;
    private IUserModel mUserModel;
    protected UserPresenter(Context context) {
        super(context);
    }

    public UserPresenter(IUserBetterView betterView,IUserLoginView loginView, IUserRegisterView registerView){
        this.mUserBetterView = betterView;
        this.mUserLoginView = loginView;
        this.mUserRegView = registerView;
        this.mUserModel = new UserModel();
    }

    @Override
    public void login(String userId, String userPass, String identify) {
        this.setBeforeQueryHook(new STNetTask.BeforeQueryHook() {
            @Override
            public void beforeQueryHook() {
                mUserLoginView.showLoding();
            }
        });
        this.setAfterQueryHook(new STNetTask.AfterQueryHook() {
            @Override
            public void afterQueryHook() {
                mUserLoginView.hideLoding();
            }
        });
        String url = getUrl(identify + "Login");
        mUserModel.login(url, userId, userPass, identify, this, this.beforeQueryHook, this.afterQueryHook);

    }

    @Override
    public void register(String userId, String userPass, String userName, String userMajor,
                         String userEmail, String userClass, String identify) {
        this.setBeforeQueryHook(new STNetTask.BeforeQueryHook() {
            @Override
            public void beforeQueryHook() {
                mUserRegView.showLoding();
            }
        });
        this.setAfterQueryHook(new STNetTask.AfterQueryHook() {
            @Override
            public void afterQueryHook() {
                mUserRegView.hideLoding();
            }
        });
        String url = getUrl(identify + "Reg");
        mUserModel.register(url, userId, userPass, userName, userMajor, userEmail, userClass, identify,
                this, this.beforeQueryHook, this.afterQueryHook);
    }

    @Override
    public void logout() {
        this.setBeforeQueryHook(new STNetTask.BeforeQueryHook() {
            @Override
            public void beforeQueryHook() {
                mUserBetterView.showLoding();
            }
        });
        this.setAfterQueryHook(new STNetTask.AfterQueryHook() {
            @Override
            public void afterQueryHook() {
                mUserBetterView.hideLoding();
            }
        });
        String identify = mUserModel.getIdentify();
        String userId = mUserModel.getId();
        String url = getUrl(identify + "Logout");
        mUserModel.logout(url, userId, identify,this, beforeQueryHook, afterQueryHook);
    }

    @Override
    public void setUserInfo(Context context, int[]layoutArray, ListView listView) {
        Map<String, String> params = mUserModel.getUserInro();
        List<UserInfo> list = new ArrayList<>();
        //initUserInfo(list, params, mUserModel.getIdentify());
        AdapterBase<UserInfo> adapter = new AdapterUser(context, layoutArray, list);
        listView.setAdapter(adapter);

    }

    /*private void initUserInfo(List<UserInfo> list, Map<String, String> params, String identify) {
        if (identify.equals("teacher")) {
            UserInfo info1 = new UserInfo("姓名", params.get("teacherName"));
            UserInfo info2 = new UserInfo("所属学院", params.get("teacherMajor"));
            UserInfo info3 = new UserInfo("邮箱", params.get("teacherEmail"));
            list.add(info1);
            list.add(info2);
            list.add(info3);
        }
        else{
            UserInfo info1 = new UserInfo("姓名", params.get("studentName"));
            UserInfo info2 = new UserInfo("学院", params.get("studentMajor"));
            UserInfo info3 = new UserInfo("专业班级", params.get("studentClass"));
            UserInfo info4 = new UserInfo("邮箱", params.get("studentEmail"));
            list.add(info1);
            list.add(info2);
            list.add(info3);
            list.add(info4);
        }
    }*/
    public void saveRadioCheck(boolean teacher, boolean student){
        if(teacher){
            mUserModel.saveRadioCheck("teacher");
        }
        else if(student){
            mUserModel.saveRadioCheck("student");
        }
    }

    public String getIdentify(){
        return mUserModel.getRadioCheck();
    }

    private String getUrl(String type) {
        String url = " ";
        switch (type){
            case "teacherLogin":
                url = URLConstant.TEACHER_LOGIN;
                break;
            case "studentLogin":
                url =  URLConstant.STUDENT_LOGIN;
                break;
            case "teacherReg" :
                url = URLConstant.TEACHER_REGISTER;
                break;
            case "studentReg" :
                url = URLConstant.STUDENT_REGISTER;
                break;
            case "studentLogout" :
                url = URLConstant.STUDENT_LOGOUT;
                break;
            case "teacherLogout" :
                url = URLConstant.TEACHER_LOGOUT;
            default:
                break;
        }
        return url;
    }

    @Override
    public void onLoginSuccess() {
        mUserLoginView.showLoginSuc();

    }

    @Override
    public void onLoginFailure(String errorReason) {
        mUserLoginView.showLoginFail(errorReason);

    }

    @Override
    public void onRegSuccess() {
        mUserRegView.showRegSuc();
    }

    @Override
    public void onRegFailure(String errorReason) {
        mUserRegView.showRegFail(errorReason);

    }

    @Override
    public void onLogoutSuccess() {
        mUserBetterView.showLogoutSuc();
    }

    @Override
    public void onLogoutFailure(String errorReason) {
        mUserBetterView.showLogoutFail(errorReason);

    }
}
